var prefix = jsctx + "/memberAccount";

var memberAccountRecord = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listMemberAccountRecord",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            memberId: $("#memberId").val().trim(),
                            recordType: $("#recordType").val().trim(),
                            startTime: $("#startTime").val().trim(),
                            endTime: $("#endTime").val().trim(),
                            localTradeNo: $("#localTradeNo").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },

                        {field: 'nickname', title: '昵称', width: '100', align: 'left'},
                        {field: 'mobile', title: '手机号', width: '100', align: 'left'},
                        {field: 'localTradeNo', title: '本地交易单号', width: '100', align: 'left'},
                        {
                            field: 'accountType', title: '支付的账户类型', width: '100',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span class="label label-success">个人钱包</span>'
                                } else if (value == '1') {
                                    return '<span class="label label-info">企业钱包</span>'
                                }
                            }
                        },
                        {
                            field: 'recordType', title: '记录类型', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '<span >会员在线充值</span>'
                                } else if (value == '1') {
                                    return '<span>充电支出</span>'
                                } else if (value == '2') {
                                    return '<span>转账收入</span>'
                                } else if (value == '3') {
                                    return '<span>分配余额</span>'
                                } else if (value == '4') {
                                    return '<span>后台订单调整</span>'
                                } else if (value == '5') {
                                    return '<span>平台赠送</span>'
                                } else if (value == '6') {
                                    return '<span>积分兑换</span>'
                                } else if (value == '7') {
                                    return '<span>回收金额</span>'
                                } else if (value == '8') {
                                    return '<span>平台钱包充值</span>'
                                } else if (value == '9') {
                                    return '<span>分销返利</span>'
                                } else if (value == '10') {
                                    return '<span>资源占用费</span>'
                                } else if (value == '11') {
                                    return '<span>平台扣费</span>'
                                } else if (value == '12') {
                                    return '<span>数据迁移</span>'
                                } else if (value == '13') {
                                    return '<span>共享额度变更</span>'
                                } else if (value == '14') {
                                    return '<span>后台退款</span>'
                                }
                            }
                        },
                        {
                            field: 'paymentType', title: '支付方式', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (row.recordType == '0') {
                                    if (value == '0') {
                                        return '<span >微信支付</span>'
                                    } else if (value == '1') {
                                        return '<span>支付宝</span>'
                                    } else if (value == 'Z0') {
                                        return '<span>中行微信</span>'
                                    } else if (value == ' Z1') {
                                        return '<span>中行支付宝</span>'
                                    } else if (value == 'U') {
                                        return '<span>银联支付</span>'
                                    } else if (value == 'C') {
                                        return '<span>农行支付</span>'
                                    }
                                }
                            }
                        },
                        {
                            field: 'refundStatus', title: '退款状态', width: '100',
                            formatter: function (value, row, index) {
                                if (row.recordType == '0') {
                                    if (value == '0') {
                                        return '<span class="label label-success">未退款</span>'
                                    } else if (value == '1') {
                                        return '<span class="label label-info">退款中</span>'
                                    } else if (value == '2') {
                                        return '<span class="label label-info">已退款</span>'
                                    } else if (value == '3') {
                                        return '<span class="label label-info">驳回</span>'
                                    }
                                }
                            }
                        },
                        {field: 'amount', title: '变更余额', width: '100', align: 'right'},
                        {field: 'result', title: '变更后余额', width: '100', align: 'right'},
                        {field: 'createdTime', title: '变动时间', width: '100', align: 'right'},
                        {field: 'remark', title: '备注', width: '100', align: 'left'},
                        {
                            title: '操作',
                            field: 'accountRecordId',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a;
                                if (row.recordType == '0') {
                                    if (value == '0') {
                                        a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="申请退款" onclick="memberAccountRecord.refundAmount(\''
                                            + row.accountRecordId
                                            + '\')">申请退款</a> '
                                    } else if (value == '1') {
                                        a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="退款中" onclick="memberAccountRecord.refundAmount(\''
                                            + row.accountRecordId
                                            + '\')">退款中</a> '
                                    } else if (value == '2') {
                                        a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="退款详情" onclick="memberAccountRecord.refundAmount(\''
                                            + row.accountRecordId
                                            + '\')">退款详情</a> '
                                    } else if (value == '3') {
                                        a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="驳回" onclick="memberAccountRecord.refundAmount(\''
                                            + row.accountRecordId
                                            + '\')">驳回</a> '
                                    }
                                }
                                return a;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        location.href = prefix + "/index";
    }

    function changeRecord(memberId) {
        location.href = prefix + "/changeRecord";
    }

    //申请退款
    function refundAmount(accountRecordId) {
        location.href = prefix + "/refundAmount/" + accountRecordId;
    }

    function exportMemberAmountRecord() {
        $.fileDownload(jsctx + "/enterpriseMember/exportSubMemberAmountRecord", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                memberId: $("#memberId").val().trim(),
                startTime: $("#startTime").val().trim(),
                recordType: $("#recordType").val().trim(),
                endTime: $("#endTime").val().trim(),
                localTradeNo: $("#localTradeNo").val().trim(),
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        changeRecord: changeRecord,
        refundAmount: refundAmount,
        exportMemberAmountRecord: exportMemberAmountRecord
    };
})();
